package com.briup.run.web.servlet;

import com.briup.run.bean.Memberinfo;
import com.briup.run.constant.AutoLoginConstant;
import com.briup.run.service.MemberInfoService;
import com.briup.run.service.impl.MemberInfoServiceImpl;
import com.briup.run.util.IpUtil;
import com.briup.run.util.MD5Util;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: vanse(lc))
 * @Date: 2022/10/7-10-07-16:25
 * @Description：${PACKAGE_NAME}
 */
@WebServlet("/login")
public class MemberInfoLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 验证码 TODO
        // 1.接收数据
        String username = request.getParameter("username");
        String password = request.getParameter("password"); // 非加密
        MemberInfoService memberInfoService = new MemberInfoServiceImpl();
        // 2.根据业务层是否报错 跳转页面
        String url="member/activity.jsp";
        HttpSession session = request.getSession();
        try {
            // 密码是加密
            Memberinfo memberinfo = memberInfoService.login(username, MD5Util.encode(password));
            session.setAttribute("memberinfo",memberinfo);
            // 记录cookie
            String autoLogin = request.getParameter(AutoLoginConstant.AUTOLOGIN);
            // 勾选了自动登录
            if("0".equals(autoLogin)){
                Cookie nameCookie = new Cookie(AutoLoginConstant.AUTO_NAME,memberinfo.getNickname());
                Cookie passwordCookie = new Cookie(AutoLoginConstant.AUTO_PASS,memberinfo.getPassword());
                nameCookie.setMaxAge(AutoLoginConstant.AUTO_EXPIRY);
                passwordCookie.setMaxAge(AutoLoginConstant.AUTO_EXPIRY);
                // 写回到客户都安
                response.addCookie(nameCookie);
                response.addCookie(passwordCookie);
            }

            //获取ServletContext对象
            ServletContext application = session.getServletContext();

            List onlineUserList = (List) application.getAttribute("onlineUserList");
            if (onlineUserList == null||onlineUserList.equals("")) {
                onlineUserList = new ArrayList();
                application.setAttribute("onlineUserList",onlineUserList);
            }
            onlineUserList.add(username);



            Object count = application.getAttribute("count");

            //2. 判断count参数是否存在，如果不存在，则初始化，设置为0；反之，加 1
            if (count == null) {
                count = 0;
                application.setAttribute("count", count);
            }

            int number = (int) count + 1;
            System.out.println("number: " + number);
            application.setAttribute("count", number);

        } catch (Exception e) {
            e.printStackTrace();
            url = "login.jsp";
            session.setAttribute("error",e.getMessage());
        }
        // 3.跳转页面
        response.sendRedirect(url);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

